using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;

namespace QuanLyTruongHoc_App
{
    public partial class SysConfig : Form
    {
        //public const string SYS_PATH = "C:\\WINDOWS\\";
        private const String FILE_CONFIG = "qlth.config";

        public delegate void eventSaveClick(object sender);
        public event eventSaveClick saveClick;
        
        public SysConfig()
        {
            InitializeComponent();
            this.txt_db_provider.Text = "SQLOLEDB";
        }

        public static String getConfigFilePath()
        {
            return Application.StartupPath + "\\" + FILE_CONFIG;
        }

        private void checkUserPower()
        {
            if (PublicLib.CURR_USER.Length == 0) return;

            int pow = PublicLib.getUserPower(PConst.QUYEN_CAU_HINH_CSDL);
            // user co quyen view
            //tscb_User.Enabled = ((pow & PConst.POW_VIEW) > 0);
            // user co quyen them
            //tsb_TaoMoi.Enabled = ((pow & PConst.POW_ADD) > 0);
            // user co quyen edit
            txt_db_server.ReadOnly = txt_db_name.ReadOnly = txt_db_user.ReadOnly = 
                txt_db_pass.ReadOnly = !((pow & PConst.POW_EDIT) > 0);

            bt_Luu.Enabled = ((pow & PConst.POW_EDIT) > 0);
            // user co delete
            //tsb_Delete.Enabled = ((pow & PConst.POW_DEL) > 0);
        }

        private void bt_logIn_Click(object sender, EventArgs e)
        {
            if (!testConnect())
                return;

            FileInfo t = new FileInfo(FILE_CONFIG);
            StreamWriter w = t.CreateText();
            w.WriteLine(CryptorEngine.Encrypt(txt_db_provider.Text, true));
            w.WriteLine(CryptorEngine.Encrypt(txt_db_server.Text, true));
            w.WriteLine(CryptorEngine.Encrypt(txt_db_name.Text, true));
            w.WriteLine(CryptorEngine.Encrypt(txt_db_user.Text, true));
            w.WriteLine(CryptorEngine.Encrypt(txt_db_pass.Text, true));
            w.Close();
            this.Visible = false;

            try
            {
                saveClick(this);
            }
            catch { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private String formatStrConn(String _conn)
        {
            String result = "";
            for (int i = 0; i < _conn.Length; i++)
            {
                if (_conn[i] == '\\') 
                {
                    result += "\\";
                }
                result += _conn[i];
            }

            return result;
        }

        private void checkTextBox(TextBox _tb)
        {
            if (_tb.Text == "")
            {
                MessageBox.Show("Hãy nhập vào giá trị", "Loi");
                _tb.Focus();
            }
        }
        private bool testConnect()
        {
            System.Data.OleDb.OleDbConnection objConn;
            String strConn = "";
            checkTextBox(txt_db_provider);
            checkTextBox(txt_db_server);
            checkTextBox(txt_db_name);
            checkTextBox(txt_db_user);
            checkTextBox(txt_db_pass);

            strConn += "Provider = " + this.txt_db_provider.Text + ";";
            strConn += "Data Source=" + this.txt_db_server.Text + ";";
            strConn += "Initial Catalog=" + this.txt_db_name.Text + ";";
            strConn += "User ID=" + this.txt_db_user.Text + ";";
            strConn += "PassWord=" + this.txt_db_pass.Text;
            //strConn = formatStrConn(strConn);
            try
            {
                objConn = new System.Data.OleDb.OleDbConnection(strConn);
                objConn.Open();
                objConn.Close();
                MessageBox.Show("Kết nối thành công!", "Thong Bao");
                return true;
            }
            catch (OleDbException)
            {
                //this.exConnect = ex.Message;
                MessageBox.Show("Kết nối không thành công!", "Thong Bao");
                return false;
            }
            //return false;
        }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            testConnect();           
        }

        private void SysConfig_Load(object sender, EventArgs e)
        {
            if (!System.IO.File.Exists(getConfigFilePath())) return;
            System.IO.StreamReader r = System.IO.File.OpenText(getConfigFilePath());
            try
            {
                //STR_CONN = "Provider = " + CryptorEngine.Decrypt(r.ReadLine(), true) + ";";
                r.ReadLine(); // provider
                txt_db_server.Text = CryptorEngine.Decrypt(r.ReadLine(), true);
                txt_db_name.Text = CryptorEngine.Decrypt(r.ReadLine(), true);
                txt_db_user.Text = CryptorEngine.Decrypt(r.ReadLine(), true);
                txt_db_pass.Text = CryptorEngine.Decrypt(r.ReadLine(), true);
                r.Close();
            }
            catch
            {
                r.Close();
            }
        }

        private void SysConfig_Activated(object sender, EventArgs e)
        {
            checkUserPower();
        }
    }
}